package com.jindy.module.customer.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.jindy.common.core.domain.entity.SysDept;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.DateUtils;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.customer.domain.Customer;
import com.jindy.module.customer.dto.CustomerDto;
import com.jindy.module.customer.service.ICustomerService;
import com.jindy.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户Controller
 *
 * @author ruoyi
 * @date 2022-09-19
 */
@RestController
@RequestMapping("/module/customer")
public class CustomerController extends BaseController
{
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询客户列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('module:customer:list')")
    public TableDataInfo list(CustomerDto customer)
    {
//        //添加查看权限
//        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
//        customer.setUserId(sysUser.getUserId());

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SysDept dept =sysUser.getDept();
        if(dept.getDeptName().indexOf("市场")>=0){
            customer.setUserId(sysUser.getUserId());
        }

        if("1".equals(customer.getSearch())){
            //今日新增
            customer.setCreateTime(DateUtils.getNowDate());
        }else if("2".equals(customer.getSearch())){
            //查询我负责的
//            customer.setChargePersonId(SecurityUtils.getUserId().toString());
//            SysUser sysUser1 = SecurityUtils.getLoginUser().getUser();
//            customer.setUserId(sysUser1.getUserId());

            SysDept dept1 =sysUser.getDept();
            if(dept1.getDeptName().indexOf("市场")>=0){
                customer.setUserId(sysUser.getUserId());
            }
        }
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 模糊查询客户列表
     */
    @GetMapping("/list2")
    @PreAuthorize("@ss.hasPermi('module:customer:list')")
    public TableDataInfo list2(CustomerDto customer)
    {
        //添加查看权限
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        customer.setUserId(sysUser.getUserId());

        startPage();
        List<Customer> list = customerService.selectCustomerList2(customer);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('module:customer:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerDto customer)
    {
        //添加权限
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        customer.setUserId(sysUser.getUserId());

        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @GetMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPermi('module:customer:id')")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(customerService.selectCustomerById(id));
    }


    /**
     * 下载模板
     */
//    @RequiresPermissions("system:student:view")
//    @PreAuthorize("@ss.hasPermi('module:customer:view')")
    @PostMapping("/importTemplate")
//    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.importTemplateExcel(response,"客户信息数据");
    }

    /**
     * 批量导入客户信息数据
     */
//    @RequiresPermissions("system:customer:import")
    @Log(title = "导入客户", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('module:customer:import')")
    @PostMapping("/importCustomer")
//    @ResponseBody
    public AjaxResult importCustomer(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        List<Customer> customerList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = customerService.importCustomer(customerList, updateSupport, operName);
        return AjaxResult.success(message);
    }


    /**
     * 新增客户
     */
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('module:customer:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer)
    {
        //添加权限
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        customer.setChargePersonId(sysUser.getUserId().toString());
        customer.setChargePersonName(sysUser.getUserName());
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户
     */
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:customer:update')")
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer)
    {
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 批量修改客户
     */
    @Log(title = "客户", businessType = BusinessType.UPDATE)
//    @PreAuthorize("@ss.hasPermi('module:customer:update2')")
    @PutMapping("/update2")
    public void edit2(@RequestBody Customer customer ) {
        customerService.updateCustomers(customer);
    }


    /**
     * 根据传过来的list批量修改客户
     */
    @Log(title = "客户", businessType = BusinessType.UPDATE)
//    @PreAuthorize("@ss.hasPermi('module:customer:update')")
    @PutMapping("/update")
    public void edits(@RequestParam List<Customer> customerList){
        customerService.updateCustomersByList(customerList);
    }


    /**
     * 删除客户
     */
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:customer:delete')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(customerService.deleteCustomerByIds(ids));
    }
}
